package app.system.controll;

import app.system.entity.MusicMusicLisit;
import app.system.entity.SysMusicList;
import app.system.entity.SysUser;
import app.system.service.MusicMusicListService;
import app.system.service.SysMusicListService;
import app.utils.Result.AjaxResult;
import app.utils.SoleID;
import app.utils.VeDate;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class MusicMusicListControll {

    @Autowired
    private MusicMusicListService musicMusicListService;
    @Autowired
    private SysMusicListService musicListService;


    @ResponseBody
    @PostMapping("creatMusicList")
    public AjaxResult creatMusicList(@RequestBody List<SysMusicList> musicLists){
        if (musicLists.size()<1){
            return AjaxResult.error("传入的数据为空");
        }else {
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            String listId = SoleID.getId();
            SysMusicList musicList = musicLists.get(0);
            musicList.setList_id(listId);
            musicList.setList_creatdate(VeDate.dateToStrLong(new Date()));
            musicList.setList_creatorid(sysUser.getUser_id());
            musicListService.saveMusicList(musicList);

            for (int i=0;i<musicLists.size();i++){
                MusicMusicLisit musicMusicLisit = new MusicMusicLisit();
                musicMusicLisit.setMusic_id(musicLists.get(i).getMusic_id());
                musicMusicLisit.setMusiclist_id(listId);
                musicMusicListService.saveMusicMusicList(musicMusicLisit);
            }
            return null;
        }
    }

    @ResponseBody
    @PostMapping("editMusicList")
    public AjaxResult editMusicList(@RequestBody List<SysMusicList> musicLists){
        if (musicLists.size()<1){
            return AjaxResult.error("传入的数据为空");
        }else {
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            String listId = SoleID.getId();
            SysMusicList musicList = musicLists.get(0);
            SysMusicList sysMusicList = musicListService.selectSysMusicListById(musicList.getList_id());
            sysMusicList.setList_name(musicList.getList_name());
            sysMusicList.setList_type(musicList.getList_type());
            sysMusicList.setList_note(musicList.getList_note());
            sysMusicList.setList_photourl(musicList.getList_photourl());
            musicListService.updateMusicListByTage(sysMusicList);
            MusicMusicLisit musicMusicLisit = new MusicMusicLisit();
            musicMusicLisit.setMusiclist_id(sysMusicList.getList_id());
            musicMusicListService.deleteMusicMusicLisit(musicMusicLisit);
            for (int i=0;i<musicLists.size();i++){
                MusicMusicLisit musicMusicLisit1 = new MusicMusicLisit();
                musicMusicLisit1.setMusic_id(musicLists.get(i).getMusic_id());
                musicMusicLisit1.setMusiclist_id(listId);
                musicMusicListService.saveMusicMusicList(musicMusicLisit1);
            }
            return null;
        }
    }
}
